<?php
/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Team. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/docs
 */

// Data edit or update
if( $action == 'update' ||  $action == 'add' )
{
    $data_list = isset($this->data['Member'])?$this->data['Member']:$data_list;
    $data_list['id'] = isset( $data_list['id'] ) ? $data_list['id'] : "";
    $data_list['name_title'] = isset( $data_list['name_title'] ) ? $data_list['name_title'] : "";
    $data_list['f_name'] = isset( $data_list['f_name'] ) ? $data_list['f_name'] : "";
    $data_list['l_name'] = isset( $data_list['l_name'] ) ? $data_list['l_name'] : "";
    $data_list['login_name'] = isset( $data_list['login_name'] ) ? $data_list['login_name'] : "";
    $data_list['login_password'] = isset( $data_list['login_password'] ) ? $data_list['login_password'] : "";
    $data_list['email'] = isset( $data_list['email'] ) ? $data_list['email'] : "";
    $data_list['phone_no'] = isset( $data_list['phone_no'] ) ? $data_list['phone_no'] : "";
    $data_list['address'] = isset( $data_list['address'] ) ? $data_list['address'] : "";
    $data_list['state'] = isset( $data_list['state'] ) ? $data_list['state'] : "";
    $data_list['zipcode'] = isset( $data_list['zipcode'] ) ? $data_list['zipcode'] : "";
    $data_list['phone_no'] = isset( $data_list['zipcode'] ) ? $data_list['zipcode'] : "";
    $data_list['country'] = isset( $data_list['country'] ) ? $data_list['country'] : "";
    $data_list['website'] = isset( $data_list['website'] ) ? $data_list['website'] : "";
    $data_list['pay_date'] = isset( $data_list['pay_date'] ) ? $data_list['pay_date'] : "";
    $data_list['status'] = isset( $data_list['status'] ) ? $data_list['status'] : "";

    /*
     * Calling validation
     */
    echo App::Load("Helper/Javascript")->validation_map(array('_errBg'=>ERROR_BACKGROUND,'_dflBg'=>"#CECECE",'_autoSubmit'=>'true','_errorMark'=>'inline','_errToolTips'=>'false'));

    //Call POST form
    echo $this->get_tag('form',array('action'=>$this->baseurl("/member/manage/$action/" . $data_list['id']),'method'=>'post','enctype'=>'multipart/form-data','class'=>'app_form app_validation','id'=>'auto_chk_submit'));

        // Call the tools bar
        App::Helper('Toolbar')->setTitle($action . " user ")->setBtnSave()->setBtnSaveAndUpdate()->setBtnBack()->Render(array('code'=>'member_save_top'));
        echo $this->callElement('default/tools/message', array('caption' =>'Message','type' => 'error', 'message' => $error));
        
        //Generate submit form
        App::Module('DataGrid') ->setDisplay('FormListing')
                                #->addRow('Name Title',App::load("Helper/Html")->selectTag("data[Member][name_title]",array("Mr." =>"Mr.","Mrs." =>"Mrs.","Dr." =>"Dr." ),$data_list['name_title'],array('id' => 'name_title')))
                                ->addRow('First Name',App::load("Helper/Html")->inputTag("data[Member][f_name]",$data_list['f_name'],array("id"=>"f_name","class"=>"app_input check_notempty")) )
                                ->addRow('Last Name',App::load("Helper/Html")->inputTag("data[Member][l_name]",$data_list['l_name'],array("id"=>"l_name","class"=>"app_input check_notempty")) )
                                #->addRow('Phone No',App::load("Helper/Html")->inputTag("data[Member][phone_no]",$data_list['phone_no'],array("class"=>"app_input")) )
                                ->addRow('Address',App::load("Helper/Html")->textareaTag("data[Member][address]",$data_list['address'],array("class"=>"app_input check_notempty")) )
                                #->addRow('State',App::load("Helper/Html")->inputTag("data[Member][state]",$data_list['state'],array("class"=>"app_input ")) )
                                #->addRow('Zipcode',App::load("Helper/Html")->inputTag("data[Member][zipcode]",$data_list['zipcode'],array("class"=>"app_input ")) )
                                ->addRow('Country',App::load("Helper/Html")->countryTag("data[Member][country]",$data_list['country'],array('id' => 'country','class'=>"check_notempty")) )
                                #->addRow('Website',App::load("Helper/Html")->inputTag("data[Member][website]",$data_list['website'],array("class"=>"app_input")) )
                                ->addRow('Status',App::load("Helper/Html")->selectTag("data[Member][status]",array("Applied"=>"Applied","Active"=>"Active","Pending"=>"Pending"),$data_list['status']))
                                ->Render();

        // Bottom tool bar
        App::Helper('Toolbar')->setBtnSave()->setBtnSaveAndUpdate()->setBtnBack()->Render(array('code'=>'member_save_bottom'));
        
        // User id in hidden field
        echo App::load("Helper/Html")->hiddenTag("data[Member][id]",$data_list['id']);
    echo $this->get_tag("/form");
}
else if ( $action =='view')
{
    App::Helper('Toolbar')->setTitle("{$data_list['f_name']} {$data_list['l_name']}")->setBtnBack()->Render(array('code'=>'member_view_top'));
    App::Module('DataGrid')->setDisplay('FormListing')
                           // Leave off some fields for futher use
                           #->addRow("Name Title",$data_list['name_title'])
                           ->addRow("First Name",$data_list['f_name'])
                           ->addRow("Last Name",$data_list['l_name'])
                           ->addRow("Email",$data_list['email'])
                           #->addRow("Phone No",$data_list['phone_no'])
                           ->addRow("Address",App::Load("Helper/Utility")->codeFormated($data_list['address']))
                           #->addRow("State",$data_list['state'])
                           #->addRow("Zipcode",$data_list['zipcode'])
                           ->addRow("Country",App::Load("Helper/Utility")->countryCodeToName($data_list['country']))
                           #->addRow("Website",$data_list['website'])
                           ->addRow("Date Signup",App::Load("Helper/Date")->dateFormated($data_list['signup_date']))
                           ->addRow("Status",$data_list['status'])
                           ->Render();
    App::Helper('Toolbar')->setBtnBack()->Render(array('code'=>'member_view_bottom'));
}
else
{
    App::Helper('Toolbar')->setTitle("View Members")->setBtnDelete()->setBtnBack()->Render(array('code'=>'member_list_view_top'));
    if(!empty($data_list['data']))
    {
        $Grid = App::Module('DataGrid');
        $Grid->setHeader(array($this->get_tag('input',array('type'=>'checkbox','id'=>'checkall','class'=>"{'mode':'Member'}")),'#','First Name', 'Last Name', 'Email','Status','Options'));
        $Grid->setFooter($data_list['link'] . " " . $data_list['paging'] . " &nbsp;&nbsp; {$this->__('Total Entry')} : " . $data_list["total"]);
        foreach($data_list['data'] as $key=>$val)
        {
            $Grid->addRow(
                            $this->get_tag('input',array('type'=>'checkbox','value'=>"{$val['id']}","class"=>"checkrow")),
                            (($data_list["page"] - 1) * $this->get_config('default_pagination') + $key + 1),
                            $val['f_name'],
                            $val['l_name'],
                            $val['email'],
                            $val['status'],
                              App::load("Helper/Html")->linkTag($this->baseurl('/member/manage/view/' . $val['id'] ),App::load("Helper/Html")->imgTag($this->baseurl('/images/admin/view.gif'),array('title' => 'View ',NULL,'alt'=>'View',"class"=>"icon")),array("title"=>"View user information"))
                            . App::load("Helper/Html")->linkTag($this->baseurl('/member/manage/update/' . $val['id']),App::load("Helper/Html")->imgTag($this->baseurl('/images/admin/edit.gif'),NULL,array('title'=>'Edit ','alt' => 'Edit',"class"=>"icon")),array("title"=>"Edit user information"))
                            . App::load("Helper/Html")->linkTag("javascript:void(0)",App::load("Helper/Html")->imgTag($this->baseurl('/images/admin/remove.gif'),NULL,array('title'=>'Delete','alt'=>'Delete')),array('id'=>'Member','class'=>'link_delete icon', 'title'=>'Member_' . $val['id']))
                            );
        }
        $Grid->Render();
    }
    else
    {
        echo $this->callElement('default/tools/message', array('caption' =>'Message','type' => 'error', 'message' => 'No Data found for Account'));
    }
    App::Helper('Toolbar')->setBtnDelete()->setBtnBack()->Render(array('code'=>'member_list_view_bottom'));
}
